import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {StorageProvider} from "../../providers/storage/storage";
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/task/finish',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-finishtask',
    templateUrl: 'person-finishtask.html',
})
export class PersonFinishtaskPage {
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public storage: StorageProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    userInfo = this.config.userInfoHolder;
    ionViewWillEnter() {
        this.getData(null);
    }

    page = 1;
    lists = [];
    tasks=[];
    getData(infiniteScroll) {
        let loading = this.util.loading();
        loading.show();
        this.httpService.requestPostData('/user_info/my_mission', {
            token: this.config.getToken(),
            status: 2,
            page: this.page
        }, (response) => {
            loading.close();
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['lists'].length < data['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
        this.httpService.requestPostData('/mul_mission/my_mul_mission_list', {
            token: this.config.getToken(),
            status:'3,6',
            page: this.page
        }, response => {
            if (response['error'] == 200) {
                let data = response['data'];
                //全部数据 
                this.tasks = this.tasks.concat(data['lists']);
            } else {
                this.util.presentAlert(response.message);
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    pageRedirect(page,data={}){
        let loading = this.util.loading();
        loading.show();
        this.navCtrl.push(page,data).then(success=>{
            loading.close();
            if(success !== true){//页面需要登录后跳转
                this.navCtrl.push('LoginPage',data);
            }
        })
    }
}
